package com.itheima.reggie.common;

import com.baomidou.mybatisplus.core.handlers.MetaObjectHandler;
import lombok.extern.slf4j.Slf4j;
import org.apache.ibatis.reflection.MetaObject;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;

import javax.servlet.http.HttpSession;
import java.time.LocalDateTime;


@Component
@Slf4j
public class MyMetaObjectHandler implements MetaObjectHandler {


    @Override
    public void insertFill(MetaObject metaObject) {
        log.info("添加时自动填充");

        Long id = UserHolder.getUserId();
        if (metaObject.hasGetter("createTime")){
            metaObject.setValue("createTime", LocalDateTime.now());
        }

        if (metaObject.hasGetter("updateTime")){
            metaObject.setValue("updateTime",LocalDateTime.now());
        }

        if (metaObject.hasGetter("createUser")){
            metaObject.setValue("createUser",id);
        }

        if (metaObject.hasGetter("updateUser")){
            metaObject.setValue("updateUser",id);
        }

    }

    @Override
    public void updateFill(MetaObject metaObject) {

        log.info("更新时自动填充");

        Long id = UserHolder.getUserId();
        if (metaObject.hasGetter("updateTime")){
            metaObject.setValue("updateTime",LocalDateTime.now());
        }
        if (metaObject.hasGetter("updateUser")){
            metaObject.setValue("updateUser",id);
        }
    }
}
